package main

import (
	"io/ioutil"
	"log"

	simplejson "github.com/bitly/go-simplejson"
)

func readFileFrompath(path string) []byte {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		log.Println(err)
	}
	return content
}

func main() {
	log.Println("-------------------------------------------------------------------")
	log.Println("测试数据")
	log.Println("-------------------------------------------------------------------")
	payload := readFileFrompath("./resources/test.json")
	log.Println("Src Data:", string(payload))
	log.Println("-------------------------------------------------------------------")
	log.Println("New测试")
	log.Println("-------------------------------------------------------------------")
	j_temp := simplejson.New()
	data, err := j_temp.EncodePretty()
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(string(data))
	log.Println("-------------------------------------------------------------------")
	log.Println("Set测试")
	log.Println("-------------------------------------------------------------------")

	string_array := [...]string{"1", "2", "3"}
	j_temp.Set("null", nil)
	j_temp.Set("string", "string")
	j_temp.Set("number_int", 1024)
	j_temp.Set("number_float", 3.1415926)
	j_temp.Set("bool_true", true)
	j_temp.Set("bool_false", false)
	j_objectTemp := simplejson.New()
	j_objectTemp.Set("string", "string")
	j_temp.Set("object", j_objectTemp)
	j_arrayTemp := []*simplejson.Json{}
	j_arrayTemp = append(j_arrayTemp, j_objectTemp)
	j_arrayTemp = append(j_arrayTemp, j_objectTemp)
	j_arrayTemp = append(j_arrayTemp, j_objectTemp)
	j_temp.Set("array", j_arrayTemp)
	j_temp.Set("string_array", string_array)

	data, err = j_temp.EncodePretty()
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(string(data))
	log.Println("-------------------------------------------------------------------")
	log.Println("NewJson测试")
	log.Println("-------------------------------------------------------------------")
	j_temp, err = simplejson.NewJson(payload)
	if err != nil {
		log.Fatalln(err)
	}
	data, err = j_temp.EncodePretty()
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(string(data))

	log.Println("-------------------------------------------------------------------")
}
